package z_exam;

import java.util.*;

/**
 * @Author: 海琳琦
 * @Date: 2022/4/13 19:02
 */
public class Title7 {

    static class Machine{
        int id;

        int coreSize;

        int innerCache;

        //CPU架构
        int CPU;

        int isSupportGPU;

        public Machine() {
        }

        public Machine(int id, int coreSize, int innerCache, int CPU, int isSupportGPU) {
            this.id = id;
            this.coreSize = coreSize;
            this.innerCache = innerCache;
            this.CPU = CPU;
            this.isSupportGPU = isSupportGPU;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        sc.nextLine();
        //m台服务器的属性（不能一开始就放到PriorityQueue里面，因为排序规则不确定）
        List<Machine> list = new ArrayList<>();
        while (m-- > 0) {
            String[] split = sc.nextLine().split(",");
            int id = Integer.valueOf(split[0]);
            int coreSize = Integer.valueOf(split[1]);
            int innerCache = Integer.valueOf(split[2]);
            int CPU = Integer.valueOf(split[3]);
            int isSupportGPU = Integer.valueOf(split[4]);
            Machine machine = new Machine(id, coreSize, innerCache, CPU, isSupportGPU);
            list.add(machine);
        }
        int N = sc.nextInt();
        //1 策略1
        int strategy = sc.nextInt();
        int cpuCount = sc.nextInt();
        int memSize = sc.nextInt();
        int cpuArch = sc.nextInt();
        int supportNP = sc.nextInt();
        PriorityQueue<Machine> queue;
        if (strategy == 1) {
            //CPU优先
            queue = new PriorityQueue<>((a,b)->{
                if (a.CPU == b.CPU && a.innerCache == b.innerCache) {
                    return Integer.compare(a.id, b.id);
                }
                if (a.CPU == b.CPU) {
                    return Integer.compare(a.innerCache, b.innerCache);
                }
                return Integer.compare(a.CPU, b.CPU);
            });
        }else{
            queue = new PriorityQueue<>((a,b)->{
                if (a.CPU == b.CPU && a.innerCache == b.innerCache) {
                    return Integer.compare(a.id, b.id);
                }
                if (a.innerCache == b.innerCache) {
                    return Integer.compare(a.CPU, b.CPU);
                }
                return Integer.compare(a.innerCache, b.innerCache);
            });
        }
        queue.addAll(list);
        int count = 0;
        List<Integer> result = new ArrayList<>();
        while (!queue.isEmpty()) {
            Machine poll = queue.poll();
            if (poll.coreSize >= cpuCount && poll.innerCache >= memSize) {
                if (poll.CPU == cpuArch || cpuArch == 9) {
                    if (poll.isSupportGPU == supportNP || supportNP == 2) {
                        count++;
                        result.add(poll.id);
                        if (count >= N) {
                            break;
                        }
                    }
                }
            }
        }
        Collections.sort(result);
        //输出处理，最后一个不能带有空格
        if (count == 0) {
            System.out.println(count);
        }else{
            System.out.print(count +" ");
            for (int i = 0; i < result.size(); i++) {
                if (i != result.size() - 1) {
                    System.out.print(result.get(i)+" ");
                }else{
                    System.out.print(result.get(i));
                }
            }
        }
    }
}
